package ui.commodity;

import javax.swing.JPanel;

import blservice.commodity.StockBLService;
import tool.DateChooserJButton;
import tool.LongStringTransformer;
import tool.MyButton;
import tool.MyLabel;
import vo.StockVo;
import vo.UserVo;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;

import java.awt.Color;

public class StockSearchPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	StockBLService stockBL;
    UserVo user;
    private JTable table;
    private JScrollPane jsp;
    Vector<String> rowData;
	Vector<String> stockInfo;
	DateChooserJButton startDate;
	DateChooserJButton endDate;
	DefaultTableModel dtm;
	String from=null;
	String to =null;
	
	public StockSearchPanel(UserVo user) {
		
		this.setLayout(null);
		this.user=user;
		this.setOpaque(false);
		
		try {
			stockBL=BLServiceManager.getStockBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e1) {
			JOptionPane.showMessageDialog(this,  e1.getMessage());
			e1.printStackTrace();
		}
			
		table = new JTable(){
			private static final long serialVersionUID = -7633973331855462791L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		table.setBounds(47, 128, 535, 238);
		jsp = new JScrollPane(table);
		jsp.setBounds(49, 49, 625, 349);
		jsp.setVisible(true);
		this.add(jsp);
		
		JLabel lblTip = new MyLabel("请选择你要查看的时间段");
		lblTip.setForeground(Color.WHITE);
		lblTip.setBounds(116, 18, 139, 15);
		add(lblTip);
			
		JLabel label_1 = new JLabel("--");
		label_1.setBounds(377, 18, 20, 15);
		add(label_1);
		
		startDate= new tool.DateChooserJButton();
		startDate.setBounds(269, 14, 93, 23);
		add(startDate);
		
		endDate= new tool.DateChooserJButton();
	    endDate.setBounds(402, 14, 93, 23);
		add(endDate);		
        
		JButton btnCheck = new MyButton("Graphics/Button79-23.png");
		btnCheck.setText("查看");
		btnCheck.addActionListener(e->showSearchResult());
		btnCheck.setBounds(519, 14, 79, 23);
		add(btnCheck);	
		
		JButton button = new MyButton("Graphics/Button93-23.png");
		button.setText("导出");
		button.addActionListener(new tool.JTableExportListener(table, "库存查看", ""));
		button.setBounds(581, 408, 93, 23);
		add(button);
	
	}

	private void showSearchResult() {
		if(startDate.getDate().getTime()>endDate.getDate().getTime()){
			JOptionPane.showMessageDialog(this, "开始时间不能大于结束时间！");
			return;
		}
		
		Vector<String> columnNames = new Vector<String>();
		columnNames.add("商品名称");
		columnNames.add("商品型号");
		columnNames.add("入库数量");
		columnNames.add("入库金额");
		columnNames.add("出库数量");
		columnNames.add("出库金额");
		columnNames.add("进货数量");
		columnNames.add("进货金额");
		columnNames.add("销售数量");
		columnNames.add("销售金额");
		columnNames.add("库存数量");
		
		dtm=new DefaultTableModel();
		dtm.setColumnIdentifiers(columnNames);
		from=startDate.getText();
		to=endDate.getText();
		try {
			List<StockVo> voList=stockBL.search(from, to);
			if(voList==null){
				JOptionPane.showMessageDialog(this, "你输入的时间段内没有任何出入库记录\n");
			} else {
				for (int i = 0; i < voList.size(); i++) {
					stockInfo = new Vector<String>();
					stockInfo.add(voList.get(i).getCommodityName());
					stockInfo.add(voList.get(i).getCommodityModel());
					stockInfo.add(Integer.toString(voList.get(i).getIn_repertory()));
					stockInfo.add(LongStringTransformer.LongToString(voList.get(i).getIn_repertory_money()));
					stockInfo.add(Integer.toString(voList.get(i).getOut_repertory()));
					stockInfo.add(LongStringTransformer.LongToString(voList.get(i).getOut_repertory_money()));
					stockInfo.add(Integer.toString(voList.get(i).getImportNum()));
					stockInfo.add(LongStringTransformer.LongToString(voList.get(i).getImport_money()));
					stockInfo.add(Integer.toString(voList.get(i).getSaleNum()));
					stockInfo.add(LongStringTransformer.LongToString(voList.get(i).getSale_money()));
					stockInfo.add(Integer.toString(voList.get(i).getStock_num()));

					dtm.addRow(stockInfo);
				}
			}
			table.setModel(dtm);
		} catch (RemoteException e1) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器，显示库存查看结果失败\n" + e1.getMessage());
			e1.printStackTrace();
		}			
	}
}
	
	


